package com.wbu.estatemanagement.controller;

import com.github.pagehelper.Page;
import com.wbu.estatemanagement.common.MessageConstant;
import com.wbu.estatemanagement.common.PageResult;
import com.wbu.estatemanagement.common.Result;
import com.wbu.estatemanagement.common.StatusCode;
import com.wbu.estatemanagement.domain.Car;
import com.wbu.estatemanagement.domain.Community;
import com.wbu.estatemanagement.domain.User;
import com.wbu.estatemanagement.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    CarService carService;

    @PostMapping("/userCar")
    public Result getUserCar(HttpSession httpSession){
        User user= (User) httpSession.getAttribute("user");
        Integer userId = user.getId();
        Car car=carService.findCar(userId);
        if (car!=null){
            return new Result(true, StatusCode.OK, MessageConstant.SUCCESS,car);
        }
        return new Result(true, StatusCode.ERROR, MessageConstant.FAIL);
    }

    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap){
        Page<Car> page = carService.search(searchMap);
        return new PageResult(true, StatusCode.OK, MessageConstant.COMMUNITY_SEARCH_SUCCESS,page.getResult(),page.getTotal());
    }
}
